const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}
const getUserOpenId = () => {
    wx.login({
        success: res => {
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
            wx.request({
                url: 'https://passwd.daoker.club/api/getUserId',
                method: 'get',
                data: {
                    code: res.code
                },
                success: function(res) {
                    // console.log(res.data)
                    wx.setStorageSync('userOpenId', res.data.openid)
                }
            })
        }
    })
}
const accesstoken = function(cb) {
    wx.request({
        url: 'https://passwd.daoker.club/api/getAccessToken',
        method: 'get',
        success: data => {
            return typeof cb == "function" && cb(data.data.access_token)
                // wx.setStorageSync('AccessToken', data.data.access_token)
        }
    })
}
const getUserPassAll = function(userid, cb) {
    wx.request({
        url: 'https://passwd.daoker.club/api/all',
        method: 'get',
        data: {
            userid: userid
        },
        success: res => {
            return typeof cb == "function" && cb(res)
        }
    })

}
const getLogin = function(nickName, avatarUrl) {
    wx.request({
        url: 'https://passwd.daoker.club/api/userinsart',
        method: 'get',
        data: {
            userid: wx.getStorageSync('userOpenId'),
            username: nickName,
            avatarUrl: avatarUrl
        },
        success: function(data) {
            // console.log(nickName)
            // console.log(avatarUrl)
            // console.log(data)
        }
    })
}
module.exports = {
    formatTime: formatTime,
    getUserOpenId: getUserOpenId,
    getUserPassAll: getUserPassAll,
    accesstoken: accesstoken,
    getLogin: getLogin
}